package com.yesorno.activity;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.yesorno.constants.Constants;
import com.yesorno.model.HttpResponseObject;
import com.yesorno.ui.CustomTitleActivity;
import com.yesorno.ui.MyMenuList;
import com.yesorno.utils.HttpUtils;

public class AttentionPeopleActivity extends CustomTitleActivity {

	private Button goback;

	private ListView attention_people_listview;

	private long curUserId;

	RegBtnsClickListeners rbc = new RegBtnsClickListeners();

	/**
	 * 注册页面按钮的监听类
	 * 
	 * @author samoin
	 * @date 2011-07-06
	 * @version 0.1
	 * 
	 */
	class RegBtnsClickListeners implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (v == goback) {
				Intent intent = new Intent();
				AttentionPeopleActivity.this.setResult(RESULT_OK, intent);
				AttentionPeopleActivity.this.finish();
			}
		}
	}

	@Override
	public void setTitleViews() {
		LinearLayout titleLayout = getTITLE_LAYOUT();
		// 返回按钮
		goback = CustomTitleActivity
				.createTitleLeftButton(AttentionPeopleActivity.this,
						R.string.userinfo_title_btn_goback,
						Constants.TITLE_BTN_MARGIN1);
		goback.setOnClickListener(rbc);
		titleLayout.addView((View) goback.getParent());

		String type = getIntent().getStringExtra("type");
		int titleId = R.string.attention_people_title;
		if (type.equals("fans")) {
			titleId = R.string.fans_people_title;
		}
		// 中间的资料的文字
		TextView tv = CustomTitleActivity.createTitleDesc(
				AttentionPeopleActivity.this, titleId, 0);
		titleLayout.addView((View) tv.getParent());

		TextView tv2 = CustomTitleActivity.createTitleDesc(
				AttentionPeopleActivity.this, 0, 0);
		titleLayout.addView((View) tv2.getParent());
	}

	@Override
	public void setLayoutId() {
		setContentView(R.layout.attention_people);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		curUserId = getIntent().getLongExtra("userId", 0);
		super.onCreate(savedInstanceState);
		((LinearLayout) findViewById(R.id.menus)).addView(new MyMenuList(
				AttentionPeopleActivity.this, 2));

		setMyView();

		String type = getIntent().getStringExtra("type");
		// TODO 获取该用户的对应的发布或关注的列表
		String url = Constants.ATTENTION_LIST_URL;
		if (type.equals("fans")) {
			url = Constants.FANS_LIST_URL;
		}
		HttpResponseObject hro = HttpUtils.getInfoByUrlPost(url + "userId="
				+ curUserId, AttentionPeopleActivity.this);
		try {
			JSONArray array = hro.getResult().getJSONArray("list");
			ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
			for (int i = 0; i < array.length(); i++) {
				JSONObject jsonObject = array.getJSONObject(i);
				HashMap<String, Object> map = new HashMap<String, Object>();
				map.put("attention_people_list_userlogo",
						jsonObject.get("userLogo"));
				map.put("attention_people_list_username",
						jsonObject.get("userName"));
				map.put("attention_people_list_userid",
						jsonObject.get("userId"));
				map.put("attention_people_list_btn_add", getResources()
						.getString(R.string.people_add_attention));
				map.put("attention_people_list_btn_remove", getResources()
						.getString(R.string.people_cancel_attention));
				map.put("attention_people_list_isattention",
						jsonObject.get("isAttention"));
				list.add(map);
			}
			MyButtonAdapter adapter = new MyButtonAdapter(
					AttentionPeopleActivity.this, list,
					R.layout.black_list_list, new String[] {
							"attention_people_list_userlogo",
							"attention_people_list_username",
							"attention_people_list_btn_add",
							"attention_people_list_btn_remove",
							"attention_people_list_userid",
							"attention_people_list_isattention" }, new int[] {
							R.id.attention_people_list_userlogo,
							R.id.attention_people_list_username,
							R.id.attention_people_list_btn_add,
							R.id.attention_people_list_btn_remove,
							R.id.attention_people_list_userid,
							R.id.attention_people_list_isattention });
			attention_people_listview.setAdapter(adapter);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public class MyButtonAdapter extends BaseAdapter {
		private class buttonViewHolder {
			ImageView userLogo;
			TextView userName;
			Button addBtn;
			Button cancelBtn;
			TextView userId;
			TextView isAttention;
		}

		private ArrayList<HashMap<String, Object>> mAppList;
		private LayoutInflater mInflater;
		private Context mContext;
		private String[] keyString;
		private int[] valueViewID;
		private buttonViewHolder holder;

		public MyButtonAdapter(Context c,
				ArrayList<HashMap<String, Object>> appList, int resource,
				String[] from, int[] to) {
			mAppList = appList;
			mContext = c;
			mInflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			keyString = new String[from.length];
			valueViewID = new int[to.length];
			System.arraycopy(from, 0, keyString, 0, from.length);
			System.arraycopy(to, 0, valueViewID, 0, to.length);
		}

		@Override
		public int getCount() {
			return mAppList.size();
		}

		@Override
		public Object getItem(int position) {
			return mAppList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		public void removeItem(int position, String userId) {
			mAppList.remove(position);
			this.notifyDataSetChanged();
			// TODO 发消息删除这个黑名单
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView != null) {
				holder = (buttonViewHolder) convertView.getTag();
			} else {
				convertView = mInflater.inflate(R.layout.attention_people_list,
						null);
				holder = new buttonViewHolder();
				holder.userLogo = (ImageView) (convertView
						.findViewById(valueViewID[0]));
				holder.userName = (TextView) convertView
						.findViewById(valueViewID[1]);
				holder.addBtn = (Button) convertView
						.findViewById(valueViewID[2]);
				holder.cancelBtn = (Button) convertView
						.findViewById(valueViewID[3]);
				holder.userId = (TextView) convertView
						.findViewById(valueViewID[4]);
				holder.isAttention = (TextView) convertView
						.findViewById(valueViewID[5]);
				convertView.setTag(holder);
			}

			HashMap<String, Object> appInfo = mAppList.get(position);
			if (appInfo != null) {
				String userLogo = (String) appInfo.get(keyString[0]);
				String userName = (String) appInfo.get(keyString[1]);
				String addBtn = (String) appInfo.get(keyString[2]);
				String cancelBtn = (String) appInfo.get(keyString[3]);
				String userId = appInfo.get(keyString[4]).toString();
				Boolean isAttention = (Boolean) appInfo.get(keyString[5]);
				Handler handler = new Handler();
				CustomTitleActivity.BitmapThread r = new CustomTitleActivity.BitmapThread(
						userLogo, holder.userLogo, true);
				handler.post(r);
				holder.userName.setText(userName);
				holder.addBtn.setText(addBtn);
				holder.cancelBtn.setText(cancelBtn);
				holder.addBtn.setOnClickListener(new BlackListButtonListener(
						position));
				holder.cancelBtn
						.setOnClickListener(new BlackListButtonListener(
								position));
				holder.userId.setText(userId);
				if (isAttention) {
					holder.cancelBtn.setVisibility(View.VISIBLE);
				} else {
					holder.addBtn.setVisibility(View.VISIBLE);
				}
				holder.isAttention.setText(isAttention.toString());
			}
			return convertView;
		}

		class BlackListButtonListener implements OnClickListener {
			private int position;

			BlackListButtonListener(int pos) {
				position = pos;
			}

			@Override
			public void onClick(View v) {
				int vid = v.getId();
				if (vid == holder.addBtn.getId()) {
					// TextView temp = (TextView) v;
					// TextView userId = (TextView) ((RelativeLayout) temp
					// .getParent()).findViewById(R.id.black_list_userid);
					// removeItem(position, userId.getText().toString());
					System.out.println("add");
				}
				if (vid == holder.cancelBtn.getId()) {
					// TextView temp = (TextView) v;
					// TextView userId = (TextView) ((RelativeLayout) temp
					// .getParent()).findViewById(R.id.black_list_userid);
					// removeItem(position, userId.getText().toString());
					System.out.println("cancel");
				}
			}
		}
	}

	private void setMyView() {
		attention_people_listview = (ListView) findViewById(R.id.attention_people_listview);
		// 这句代码非常重要，用来设置点击的当前项的背景
		attention_people_listview.setSelector(R.drawable.list_bg);
		// 这句代码非常重要，用来设置拖动中的背景
		attention_people_listview.setCacheColorHint(0);
	}

}
